<!doctype html>
<html lang="en-us">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>ChucK + JS => ChucKScript</title>
    <link rel="stylesheet" href="../../css/editor.css">
  </head>
  <body onresize="chuckEditor.resize();">
<div>
    <div id="program">// HID 2: Mouse
// name: mouse-fm.ck
// desc: uses first X/Y axes of a mouse to control mf and index for FM
// author: Spencer Salazar

global Event mouseMoved, mouseDown, mouseUp;
global int mouseDeltaX, mouseDeltaY;
// modulator to carrier
SinOsc m => SinOsc c => Envelope e => dac;

// carrier frequency
220 => c.freq;
// modulator frequency
550 => m.freq;
// index of modulation
1000 => m.gain;

// phase modulation is FM synthesis (sync is 2)
2 => c.sync;

// attack
10::ms => e.duration;
.5 => e.gain;
// variables
int base;
float a0;
float a1;
float a2;
int count;

// start things
set( base, a0, a1, a2 );


fun void RespondToMouseMovement()
{
    while( true )
    {
        mouseMoved => now;
        mouseDeltaX * .001 + a0 => a0;
        mouseDeltaY * .001 + a1 => a1;
        set( base, a0, a1, a2 );
    }
}
spork ~ RespondToMouseMovement();

fun void RespondToMouseClickDown()
{
    while( true )
    {
        mouseDown => now;
        count++;
        if( count == 1 ) e.keyOn();
        set( base, a0, a1, a2 );
    }
}
spork ~ RespondToMouseClickDown();

fun void RespondToMouseClickUp()
{
    while( true )
    {
        mouseUp => now;
        count--;
        if( count <= 0 )
        {
            e.keyOff();
            0 => count;
        }
    }
}
spork ~ RespondToMouseClickUp();

// infinite time loop
while( true )
{
    0.1::second => now;
    <<< "carrier:", c.freq(), "modulator:", m.freq(), "index:", m.gain() >>>;
}
// mapping function
fun void set( int base, float v0, float v1, float v2 )
{
    // modulator frequency
    ( 500 + 5*base + ( 500 * v0) ) => m.freq;
    // carrier frequency
    ( 220 + (220 * v2) ) => c.freq;
    // index of modulation
    ( 1000 * (v1+1) ) => m.gain;
    //<<< "carrier:", c.freq(), "modulator:", m.freq(), "index:", m.gain() >>>;
}            
</div>
</div>
<div class="buttons">
    <button class="chuckButton" id="startChuck" disabled="disabled">Start ChucK</button>
    <button class="chuckButton" id="compileButton" disabled="disabled">Compile and Run</button>
    <button class="chuckButton" id="replaceButton" disabled="disabled">Replace</button>
    <button class="chuckButton" id="removeButton" disabled="disabled">Remove</button>
    <button class="chuckButton" id="clearButton" disabled="disabled">Clear VM and Globals</button>
    <button class="chuckButton" id="micButton" disabled="disabled">Enable Microphone</button>
    <button class="chuckButton" id="fileFormButton">Show File Uploader</button>
</div>
<div id="fileForm" class="hidden">
    <p>File name to use in ChucK: <input type="text" id="uploadFilename"></p>
    <p>Local file to upload: <input type="file" id="uploadFile" name="uploadFile"></p>
    <p><button class="chuckButton" id="fileButton" disabled="disabled">Upload File to Virtual File System</button></p>
</div>
<div>
    <textarea id="output"></textarea>
    <div id="shreds">
        <table id="shredstable">
            <col width="60px">
            <col width="1*">
            <col width="50px">
            <col width="60px">
            <tr><th>shred</th><th>code</th><th>time</th><th>remove</th>
        </table>
    </div>
    <div class="clearfix"></div>
</div>
    <script type="text/javascript" src="../../js/ace-min-noconflict/ace.js" charset="utf-8"></script>
    <script type="text/javascript" src="../../js/editor.js"></script>
    <script type="text/javascript" src="../../js/defer.js"></script>
    <script>
        // override
        var whereIsChuck = "../../js";
    </script>
    <script type="text/javascript" src="../../js/webchuck_host.js"></script>
    <script type="text/javascript" src="../../js/host.js"></script>
    <script>
        theChuckReady.then( function() 
        {
            var prevX, prevY;
            window.addEventListener( "mousemove", function( e )
            {
                var x = (window.Event) ? e.pageX : event.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
                var y = (window.Event) ? e.pageY : event.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
                theChuck.setInt( "mouseDeltaX", x - prevX );
                theChuck.setInt( "mouseDeltaY", y - prevY );
                theChuck.broadcastEvent( "mouseMoved" );
                prevX = x;
                prevY = y;
            });
            window.addEventListener( "mousedown", function( e )
            {
                theChuck.broadcastEvent( "mouseDown" );
            });
            window.addEventListener( "mouseup", function( e )
            {
                theChuck.broadcastEvent( "mouseUp" );
            });
        });
    </script>
  </body>
</html>


